package main

import (
	"fmt"
	"ia/apps/admin/assets/conf"
	"ia/apps/admin/assets/static"
	"ia/apps/admin/middleware/auth"
	"ia/apps/admin/web/route"
	"ia/common/storage"
	"ia/common/support"
	"ia/common/support/define"
	"ia/common/support/tools"

	"github.com/go-playground/validator/v10"
	"github.com/kataras/golog"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/middleware/recover"
)

func main() {
	app := boot()

	// HTTP
	route.Hub(app)

	if err := app.Run(
		iris.Addr(fmt.Sprintf("0.0.0.0:%d", define.GConfig.App.Own.Port)),
		iris.WithConfiguration(define.GConfig.App.Configuration),
	); err != nil {
		golog.Fatalf("IrisAdmin启动失败,原因:%v", err)
	}
}

func boot() *iris.Application {
	// init
	define.InitGlobalConfig(conf.Asset)
	support.InitLog()
	tools.InitGid()
	// storage.InitRedis()
	storage.InitGDb1()
	auth.InitAuth()
	route.InitLogSink()

	//
	app := iris.New()
	app.Logger().SetLevel(define.GConfig.App.LogLevel)
	app.Use(recover.New())
	app.Validator = validator.New()

	app.HandleDir("/", static.AssetFile())
	app.HandleDir("/static", static.AssetFile())

	return app
}
